﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharpObjects.SearchEngine.HtmlParsing.Html;

namespace SharpObjects.SearchEngine.HtmlParsing.Parser
{
    public class HtmlTagFactory
    {
        private delegate HtmlTag CreateInstanceDelegate(HtmlGenericTag tag);

        private static Dictionary<string, CreateInstanceDelegate> factory = new Dictionary<string, CreateInstanceDelegate>();

        static HtmlTagFactory()
        {
            factory.Add(HtmlAncorTag.TagName, HtmlAncorTag.CreateInstance);
            factory.Add(HtmlBodyTag.TagName, HtmlBodyTag.CreateInstance);
            factory.Add(HtmlDivTag.TagName, HtmlDivTag.CreateInstance);
            factory.Add(HtmlHeadingTag.TagName1, HtmlHeadingTag.CreateInstance);
            factory.Add(HtmlHeadingTag.TagName2, HtmlHeadingTag.CreateInstance);
            factory.Add(HtmlHeadingTag.TagName3, HtmlHeadingTag.CreateInstance);
            factory.Add(HtmlHeadingTag.TagName4, HtmlHeadingTag.CreateInstance);
            factory.Add(HtmlHeadingTag.TagName5, HtmlHeadingTag.CreateInstance);
            factory.Add(HtmlHeadingTag.TagName6, HtmlHeadingTag.CreateInstance);
            factory.Add(HtmlHeadTag.TagName, HtmlHeadTag.CreateInstance);
            factory.Add(HtmlImageTag.TagName, HtmlImageTag.CreateInstance);
            factory.Add(HtmlListItemTag.TagName, HtmlListItemTag.CreateInstance);
            factory.Add(HtmlListTag.TagNameDL, HtmlListTag.CreateInstance);
            factory.Add(HtmlListTag.TagNameOL, HtmlListTag.CreateInstance);
            factory.Add(HtmlListTag.TagNameUL, HtmlListTag.CreateInstance);
            factory.Add(HtmlMetaTag.TagName, HtmlMetaTag.CreateInstance);
            factory.Add(HtmlParagraphTag.TagName, HtmlParagraphTag.CreateInstance);
            factory.Add(HtmlSelectOptionTag.TagName, HtmlSelectOptionTag.CreateInstance);
            factory.Add(HtmlSelectTag.TagName, HtmlSelectTag.CreateInstance);
            factory.Add(HtmlTableCaptionTag.TagName, HtmlTableCaptionTag.CreateInstance);
            factory.Add(HtmlTableCellTag.TagName, HtmlTableCellTag.CreateInstance);
            factory.Add(HtmlTableHeaderTag.TagName, HtmlTableHeaderTag.CreateInstance);
            factory.Add(HtmlTableTag.TagName, HtmlTableTag.CreateInstance);
            factory.Add(HtmlTextAreaTag.TagName, HtmlTextAreaTag.CreateInstance);
            factory.Add(HtmlTextInputTag.TagName, HtmlTextInputTag.CreateInstance);
            factory.Add(HtmlTitleTag.TagName, HtmlTitleTag.CreateInstance);
            factory.Add(HtmlText.TagName, HtmlText.CreateInstance);

            foreach(string tagName in HtmlInlineTag.TagNames)
            {
                factory.Add(tagName, HtmlInlineTag.CreateInstance);
            }

            factory.Add(HtmlScriptTag.TagName, HtmlScriptTag.CreateInstance);
            factory.Add(HtmlLinkTag.TagName, HtmlLinkTag.CreateInstance);
            factory.Add(HtmlCommentTag.TagName, HtmlCommentTag.CreateInstance);
            factory.Add(HtmlStyleTag.TagName, HtmlStyleTag.CreateInstance);
            factory.Add(HtmlSpanTag.TagName, HtmlSpanTag.CreateInstance);


        }

        public HtmlTag CreateTag(HtmlGenericTag htmlGenericTag)
        {
            if(factory.ContainsKey(htmlGenericTag.Name))
            {
                return factory[htmlGenericTag.Name](htmlGenericTag);
            }

            //if(htmlGenericTag)

            return new HtmlUnknownTag(htmlGenericTag);;
        }

    }
}
